#include "user.h"

UNode* uhead=NULL;
int userofnum=0;

//创建节点
UNode* creatUNode(User data){
	UNode* pNew = (UNode*)malloc(SIZE_USER);
	//异常处理
	if(NULL == pNew) return NULL;
	//初始化
	pNew->data=data;
	pNew->pLeft=pNew->pRight=NULL;
	return pNew;
}

//节点尾插
void tailUNode(UNode** phead,User data){
	//异常处理
	if(NULL == phead) return;
	UNode* ptail=creatUNode(data);
	if(NULL == *phead){//链表为空
		*phead=ptail;
		userofnum++;
	}else{//链表不为空，找到尾
		UNode* ptemp=*phead;
		while(ptemp->pRight){
			ptemp=ptemp->pRight;
		}
		ptemp->pRight=ptail;
		ptail->pLeft=ptemp;
		userofnum++;
	}

}
//遍历所有用户
void travelUNode(UNode* phead){
	//异常处理
	if(NULL == phead){
		printf("库中未存储用户信息\n");
		jindu(1);
		managerMenuOfUser();
	} 
	while(phead){
		printf("账号：%s-密码：%s\n",phead->data.account,phead->data.password);
			phead=phead->pRight;
	}
	printf("\n");
	printf("共有用户%d个\n",userofnum);
	jindu(4);
	managerMenuOfUser();
}
//管理员添加用户
void addUser(){
	system("clear");
	printf("图书管理系统--管理员--添加用户\n");
	User data;

	getchar();
	printf("请输入用户账号：\n");
	fgets(data.account, sizeof(data.account), stdin);  
    data.account[strcspn(data.account, "\n")] = 0; 
    printf("请输入账号密码：\n");
	//getchar();
	scanf("%s",data.password);

	tailUNode(&uhead,data);
	printf("添加成功\n");
	saveUser();
	jindu(1);
	managerMenuOfUser();
}
//查找用户
UNode* findUNode(char* accountnum){
	UNode* ptemp=uhead;
	while(ptemp){
		if(!strcmp(ptemp->data.account,accountnum)) return ptemp;
		ptemp=ptemp->pRight;
	}
	return NULL;
}
//查找用户账号
void findUser(){
	system("clear");
	printf("管理员--用户管理\n");
	printf("请输入要找的用户账号\n");
	char finduseraccount[21];
	scanf("%s",finduseraccount);
	UNode* pfindaccount=findUNode(finduseraccount);
	//异常判断
	if(NULL == pfindaccount){
		printf("查找失败，请检查账号是否正确\n");
		jindu(1);
		managerMenuOfUser();
	}else{
		printf("查找账号信息为：账号-%s 密码-%s\n",
			pfindaccount->data.account,pfindaccount->data.password);
		jindu(2);
		managerMenuOfUser();
	}
}
//修改用户密码
void changeUserPassord(){
	system("clear");
	printf("管理员--修改用户信息\n");
	printf("请输入要修改的账号\n");
	char changeuseraccount[21];
	scanf("%s",changeuseraccount);
	UNode* pchnode=findUNode(changeuseraccount);
	//异常判断
	if(pchnode){//找到了
		printf("请输入修改后的密码\n");
		scanf("%s",pchnode->data.password);
		printf("修改成功\n");
		saveUser();
		jindu(2);
		managerMenuOfUser();
	}
	printf("请检查你输入的账号\n");
	changeUserPassord();
}
//删除用户
void delUser(){
	system("clear");
	printf("管理员--用户删除\n");
	printf("请输入要删除的账号\n");
	char delaccount[21];
	scanf("%s",delaccount);
	bool delBool = false;
	delBool=delAccount(&uhead,delaccount);
	if(delBool){//删除成功
		printf("删除成功\n");
		userofnum--;
	}else{
		printf("删除失败\n");
	}
	saveUser();
	jindu(2);
	managerMenuOfUser();
}
//删除节点,根据账号
bool delAccount(UNode ** phead,char* useraccount){
	//异常处理
	if(NULL == phead) return false;
	//找到要删除书籍编号的节点
	UNode* pdelAccount=findUNode(useraccount);
	//异常处理 看是否找到
	if(pdelAccount){//找到了
		//如果删除的是头
		if(*phead == pdelAccount && NULL != (*phead)->pRight){//删除的是头且链表不止有头
			*phead=pdelAccount->pRight;
			pdelAccount->pRight->pLeft=NULL;
			//内存释放
			free(pdelAccount);
			return true;
		}else if(*phead == pdelAccount && NULL == (*phead)->pRight){//删除的是头且链表只有头
			//头为空 释放头
			*phead=NULL;
			free(pdelAccount);
			return true;
		}else if(*phead != pdelAccount && NULL == pdelAccount->pRight){//删除的是尾
			pdelAccount->pLeft->pRight=pdelAccount->pRight;
			free(pdelAccount);
			return true;
		}else{//是中间的
			pdelAccount->pLeft->pRight=pdelAccount->pRight;
			pdelAccount->pRight->pLeft=pdelAccount->pLeft;
			free(pdelAccount);
			return true;
		}	
	}
	return false;//没找到
}
//查找密码的函数
UNode* findUNodePassount(char* findpassword){
	UNode* ptemp=uhead;
	while(ptemp){
		if(!strcmp(ptemp->data.password,findpassword)) return ptemp;
		ptemp=ptemp->pRight;
	}
	return NULL;
}
//用户借书函数
void UserBorrowBook(char* account){
	system("clear");
	printf("图书馆--借书\n");
	printf("请输入你要借的书的书名或作者\n");
	printf("1:书名\n");
	printf("2:作者\n");
	printf("3:返回\n");
	printf("4:退出\n");
	int borrowNum;
	scanf("%d",&borrowNum);
	switch(borrowNum){
	case 1: findBookByName(account);	break;
	case 2:	findBookByAuthor(account);				break;
	case 3: UserofBookBorrowandRepay(account);	break;
	case 4:	exit(-1);	   			
	default : printf("输入错误，请再次输入\n"); jindu(1);
				UserofBookBorrowandRepay(account); break;
	}

}
//通过书名查书 调用查找书名节点的函数
void findBookByName(char* account){//已知当前用户账号
	char findbookname[NAME_LEN];
	printf("请输入书名\n");
	scanf("%s",findbookname);
	Node* ppaccount=findNodebyName(findbookname);
	if(ppaccount){//找到了
		printf("书名：%s  作者：%s  出版社：%s\n",ppaccount->data.bookName,ppaccount->data.bookAuthor,
												ppaccount->data.bookPublish);
		printf("是否要借书(y/n)\n");
		char c;
		getchar();
		scanf("%c",&c);
		if('y' == c){//要借书
			//存放入BRUnode链表中
			BRBook date;
			memcpy(date.who, account, sizeof(account)); 
			date.data=ppaccount->data;
			tailBRNode(&brhead,date);
			printf("借书成功\n");
			jindu(2);
			UserofBookBorrowandRepay(account);
		}else{//不借书
			UserBorrowBook(account);
		}
	}else{
		printf("该书籍不存在,请检查输入是否出错\n");
		jindu(2);
		UserBorrowBook(account);
	}
}
//需要一个容器来存储用户的借还书信息
//通过作者查书 调用查找作者节点的函数
void findBookByAuthor(char* const account){
	char findbookauthor[NAME_LEN];
	printf("请输入作者\n");
	scanf("%s",findbookauthor);
	Node* pbooknode=findNodebyAuthor(findbookauthor);
	if(pbooknode){//找到了
		printf("书名：%s  作者：%s  出版社：%s\n",pbooknode->data.bookName,pbooknode->data.bookAuthor,
												pbooknode->data.bookPublish);
		printf("是否要借书(y/n)\n");
		char c;
		getchar();
		scanf("%c",&c);
		if('y' == c){//要借书
			//存放入BRUnode链表中
			BRBook date;
			memcpy(date.who, account, sizeof(account)); 
			date.data=pbooknode->data;
			tailBRNode(&brhead,date);
			printf("借书成功\n");
			saveBorrowAndRepay();
			jindu(2);
			UserofBookBorrowandRepay(account);
		}else{//不借书
			UserBorrowBook(account);
		}
	}else{
		printf("该书籍不存在,请检查输入是否出错\n");
		jindu(2);
		UserBorrowBook(account);
	}
}
//用户还书函数
void UserRepayBook(char* const account){
	system("clear");
	printf("图书馆--还书\n");
	//输出用户借了哪些书
	findBRNodebyAccount(brhead,account);
	char repaybookname[NAME_LEN];
	printf("请输入你要还的书籍名\n");
	scanf("%s",repaybookname);
	repaybook(repaybookname,account);
	saveBorrowAndRepay();
	jindu(2);
	UserofBookBorrowandRepay(account);
}

//存储用户信息 存到文件中
void saveUser(){
	FILE* fpwrite=fopen("user.txt","w");
	//异常处理
	if(NULL == fpwrite){
		printf("文件打开失败\n");
		return ;
	}
	UNode* pTemp=uhead;
	while(pTemp){
		fwrite(&(pTemp->data),1,sizeof(User),fpwrite);
		pTemp=pTemp->pRight;
	}
	fclose(fpwrite);
}
//从文件中读取借用户信息 组成新的链表
void readUser(UNode** phead){
	FILE* fpread=fopen("user.txt","r");
	if(NULL == fpread){
		printf("文件打开失败\n");
		return ;
	}
	int r;
	User temp;
	while(1){
		r = fread(&temp,1,sizeof(User),fpread);
		if(r <= 0) break;
		tailUNode(phead,temp);
	}
	fclose(fpread);
}